package com.g3d.objects;

import javax.microedition.khronos.opengles.GL10;

import com.g3d.core.G3DState;

import android.graphics.Bitmap;
import android.opengl.GLUtils;

public class G3DTexture 
{
	private final int _textureId;
	private final int _textureWidth;
	private final int _textureHeight;
	
	private final int _minFilter;
	private final int _magFilter;
	private final float _wrapU;
	private final float _wrapV;
	
	public G3DTexture(Bitmap image, int minFilter, int magFilter, float wrapU, float wrapV)
	{
		this(image, minFilter, magFilter, wrapU, wrapV, true);
	}

	public G3DTexture(Bitmap image, int minFilter, int magFilter, float wrapU, float wrapV, boolean premultiplyAlpha)
	{
		G3DState.g3dTextureEnabled();
		
		this._minFilter = minFilter;
		this._magFilter = magFilter;
		this._wrapU = wrapU;
		this._wrapV = wrapV;

		this._textureWidth = image.getWidth();
		this._textureHeight = image.getHeight();

	    this._textureId = G3DState.g3dGenerateTextureId();
	    
	    G3DState.g3dForceTexturePresent(this);
	    
		G3DState.g3dSetTextureParameters(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, this._minFilter);
		G3DState.g3dSetTextureParameters(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, this._magFilter);
		G3DState.g3dSetTextureParameters(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, this._wrapU);
		G3DState.g3dSetTextureParameters(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, this._wrapV);
		G3DState.g3dSetTextureEnvirament(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_MODULATE);
		
		if(premultiplyAlpha)
			GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, image, 0);
	}

	public int getTextureHardwareId()
	{
		return this._textureId;
	}
	
	public int getTextureWidth()
	{
		return this._textureWidth;
	}
	
	public int getTextureHeight()
	{
		return this._textureHeight;
	}

}
